package educrypt.gui.components;

import java.awt.Component;
import java.awt.Window;

import javax.swing.JDialog;
import javax.swing.WindowConstants;

public class EducryptDialog extends JDialog {

	private static final long serialVersionUID = 2410926707795304953L;

	private Component caller;

	public EducryptDialog(Window owner) {
		super(owner);
		this.setDefaultOptions();
	}

	public EducryptDialog(Window owner, Component caller) {
		super(owner);
		this.caller = caller;
		this.setDefaultOptions();
	}

	private void setDefaultOptions() {
		this.setModal(true);
		this.setAlwaysOnTop(true);
		this.setResizable(false);
		this.setSize(460, 300);
		this.setPreferredSize(this.getSize());

		this.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
	}

	public void open(Component caller) {
		this.caller = caller;
		this.open();
	}

	public void open() {
		if (this.caller != null) { // Set location relative to pressed button
			this.setLocationRelativeTo(caller);
		} else { // Set location relative to parent center
			Window owner = this.getOwner();
			this.setLocationRelativeTo(owner);
		}
		this.setVisible(true);
	}

	public void close() {
		this.setVisible(false);
	}

	public Component getCaller() {
		return caller;
	}

	public boolean isVisible() {
		return super.isVisible();
	}
}
